package car.aistar.service.impl;

import car.aistar.dto.CarOverview;
import car.aistar.dto.PriceCutXu;
import car.aistar.dto.XrbRecent;
import car.aistar.entity.*;
import car.aistar.service.*;
import car.aistar.util.XrbUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CarOverviewServiceImpl
 * @Description TODO
 * @Author laoxu
 * @Date 2020/10/2 12:51
 * Version 1.0
 **/

@Service("carOverviewService")
public class CarOverviewServiceImpl implements CarOverviewService {

    @Autowired
    private UserRecordsService userRecordsService;

    @Autowired
    private CarDetailService carDetailService;

    @Autowired
    private CarBrandsService carBrandsService;

    @Autowired
    private CarStyleService carStyleService;

    @Autowired
    private UserCollectService userCollectService;

    @Autowired
    private CarRecordService carRecordService;

    @Autowired
    private PriceCutService priceCutService;


    /**
     * @return java.util.List<car.aistar.dto.XrbRecent>
     * @Author laoxu
     * @Description //TODO * 交易记录表查成交时间-查出距当前时间一个月内记录-取出成交车辆id与成交价，成交时间减去上架时间车辆id查基本数据车辆详情表-车辆品牌表-车辆车系表
     * @Date 22:12 2020/10/2
     * @Param []
     **/

    @Override
    public List<XrbRecent> queryRecent() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = XrbUtil.getOneMonth();
        String day = format.format(d);//一个月前日期
        String now = format.format(new Date());//当前日期
        List<CarRecord> carRecords = carRecordService.findDate(day,now);//day
        List<XrbRecent> xrbRecentList = new ArrayList<>();//最后查询出的结果集
        for (CarRecord c : carRecords) {
            XrbRecent xrbRecent = new XrbRecent();
            Long cid = c.getcId();//成交车id

            Date time = c.getReTime();//成交时间

            xrbRecent.setId(cid);
            xrbRecent.setRePrice(c.getRePrice());//成交价格
            CarDetail carDetail = carDetailService.queryById(cid);

            Date time2 = carDetail.getCCreatedate();//上架时间

            //计算时间差，相差的天数
            long betweenDay = DateUtil.between(time2,time, DateUnit.DAY);

            xrbRecent.setBetweenDay(betweenDay);
            //品牌id
            Long brId = carDetail.getBrId();
            //车系id
            Long stId = carDetail.getStId();
            xrbRecent.setBrName(carBrandsService.queryById(brId).getBrName());
            xrbRecent.setStName(carStyleService.queryById(stId).getStName());
            xrbRecent.setcPhoto(carDetail.getCPhoto());
            xrbRecent.setcMileage(carDetail.getCMileage());
            xrbRecent.setcDrive(carDetail.getCDrive());

            //上牌时间获取年的部分
            Date date3 = carDetail.getCRegistration();
            int y = DateUtil.year(date3);
            xrbRecent.setcRegistration(y);
            xrbRecent.setcPaifang(carDetail.getCPaifang());
            xrbRecentList.add(xrbRecent);
        }
        return xrbRecentList;
    }

    @Override
    public PageInfo<XrbRecent> findByPage(String pageNow, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        List<XrbRecent> xrbRecentList = queryRecent();
        PageInfo<XrbRecent> recentPageInfo = new PageInfo<>(xrbRecentList);
        return recentPageInfo;
    }

    /**
    *@Author laoxu
    *@Description //TODO 降价提醒表 用户id查询降价表（车id,降价金额）车id查询详细信息表（其它信息）
    *@Date 14:14 2020/10/4
    *@Param [id]用户id
    *@return java.util.List<car.aistar.dto.PriceCutXu>
    **/
    @Override
    public List<PriceCutXu> queryPriceCut(Long id) {
        PriceCut priceCut = new PriceCut();
        priceCut.setUserId(id);
        List<PriceCut> priceCuts = priceCutService.queryAll(priceCut);
        List<PriceCutXu> priceCutXuList = new ArrayList<>();
        for (PriceCut cut : priceCuts) {
            //车id
            Long cid = cut.getCarId();
            //降价金额
            Double price = cut.getCutPrice();
            Double price1 = price * 10000;
            Integer a = price1.intValue();
            PriceCutXu priceCutXu = new PriceCutXu();
            priceCutXu.setId(cid);
            //降价金额转成Integer类型
            priceCutXu.setCutPrice(a);
            //车辆详情
            CarDetail carDetail = carDetailService.queryById(cid);
            //查询并插入品牌名和车系名
            String brName = carBrandsService.queryById(carDetail.getBrId()).getBrName();
            String stName = carStyleService.queryById(carDetail.getStId()).getStName();
            priceCutXu.setBrName(brName);
            priceCutXu.setStName(stName);
            //查询出其他基本信息
            priceCutXu.setcPhoto(carDetail.getCPhoto());
            priceCutXu.setcDrive(carDetail.getCDrive());
            priceCutXu.setcMileage(carDetail.getCMileage());
            priceCutXu.setcPaifang(carDetail.getCPaifang());

            Date date3 = carDetail.getCRegistration();
            int y = DateUtil.year(date3);
            priceCutXu.setcRegistration(y);

            priceCutXu.setcOprice(carDetail.getCOprice());
            priceCutXu.setcStatus(carDetail.getCStatus());
            priceCutXuList.add(priceCutXu);
        }
        return priceCutXuList;
    }

    @Override
    public PageInfo<PriceCutXu> priceCutPage(Long uid, String pageNow, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        List<PriceCutXu> priceCutXuList = queryPriceCut(uid);
        PageInfo<PriceCutXu> priceCutXuPageInfo = new PageInfo<>(priceCutXuList);
        return priceCutXuPageInfo;
    }



    /**
     * @return java.util.List<car.aistar.dto.CarOverview>
     * @Author laoxu
     * @Description //TODO 通过用户id查询并展示用户浏览的车辆基本信息
     * @Date 12:57 2020/10/2
     * @Param [uid]
     **/
    @Override
    public List<CarOverview> queryOverview(Long uid) {
        UserRecords userRecords = new UserRecords();
        userRecords.setUId(uid);
        List<UserRecords> userRecords1 = userRecordsService.queryAll(userRecords);
        //最后得到的基本信息
        List<CarOverview> carOverviewList = new ArrayList<>();
        for (UserRecords records : userRecords1) {
            CarOverview carOverview = new CarOverview();
            //获取车的id
            Long cid = records.getCId();
            //存入车id
            carOverview.setId(cid);
            //根据车id获取车品牌id-获取车品牌名
            //品牌id
            Long brId = carDetailService.queryById(cid).getBrId();
            //车系id
            Long stId = carDetailService.queryById(cid).getStId();

            carOverview.setBrName(carBrandsService.queryById(brId).getBrName());
            //根据车id获取车系id-获取车系名
            carOverview.setStName(carStyleService.queryById(stId).getStName());
            //根据车id获取车详情
            CarDetail carDetail = carDetailService.queryById(cid);
            carOverview.setcPhoto(carDetail.getCPhoto());
            carOverview.setcMileage(carDetail.getCMileage());
            carOverview.setcDrive(carDetail.getCDrive());

            Date date3 = carDetail.getCRegistration();
            int y = DateUtil.year(date3);
            carOverview.setcRegistration(y);

            carOverview.setcPaifang(carDetail.getCPaifang());
            carOverview.setcStatus(carDetail.getCStatus());
            carOverview.setcOprice(carDetail.getCOprice());
            carOverviewList.add(carOverview);
        }
        return carOverviewList;
    }

    @Override
    public PageInfo<CarOverview> findByPage1(Long uid, String pageNow, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        List<CarOverview> carOverviewList = queryOverview(uid);
        PageInfo<CarOverview> overPageInfo = new PageInfo<>(carOverviewList);
        return overPageInfo;
    }

    /**
     * @return java.util.List<car.aistar.dto.CarOverview>
     * @Author laoxu
     * @Description //TODO 收藏表记录
     * @Date 19:40 2020/10/2
     * @Param [uid]
     **/
    @Override
    public List<CarOverview> queryCollect(Long uid) {
        UserCollect userCollect = new UserCollect();
        userCollect.setUId(uid);
        System.out.println(userCollect);
        List<UserCollect> userCollectList = userCollectService.queryAll(userCollect);
        System.out.println(userCollectList);
        List<CarOverview> carOverviewList = new ArrayList<>();
        for (UserCollect collect : userCollectList) {
            CarOverview carOverview = new CarOverview();
            Long cid = collect.getCId();
            carOverview.setId(cid);
            //根据车id获取车品牌id-获取车品牌名
            //品牌id
            Long brId = carDetailService.queryById(cid).getBrId();
            //车系id
            Long stId = carDetailService.queryById(cid).getStId();

            carOverview.setBrName(carBrandsService.queryById(brId).getBrName());
            //根据车id获取车系id-获取车系名
            carOverview.setStName(carStyleService.queryById(stId).getStName());
            //根据车id获取车详情
            CarDetail carDetail = carDetailService.queryById(cid);
            carOverview.setcPhoto(carDetail.getCPhoto());
            carOverview.setcMileage(carDetail.getCMileage());
            carOverview.setcDrive(carDetail.getCDrive());

            Date date3 = carDetail.getCRegistration();
            int y = DateUtil.year(date3);
            carOverview.setcRegistration(y);

            carOverview.setcPaifang(carDetail.getCPaifang());
            carOverview.setcStatus(carDetail.getCStatus());
            carOverview.setcOprice(carDetail.getCOprice());
            carOverviewList.add(carOverview);
        }
        return carOverviewList;
    }

    @Override
    public PageInfo<CarOverview> findCollectPage(Long uid, String pageNow, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        List<CarOverview> carOverviewList = queryCollect(uid);
        PageInfo<CarOverview> viewPageInfo = new PageInfo<>(carOverviewList);
        return viewPageInfo;
    }
}
